package com.xinfu.newproject.model.park;

import android.util.Log;

import com.google.gson.Gson;
import com.xinfu.newproject.apiservice.JsNetworkService;
import com.xinfu.newproject.bean.park_bean.bean.AddMonthCarBean;
import com.xinfu.newproject.bean.park_bean.bean.CheckMonthParkBean;
import com.xinfu.newproject.bean.park_bean.bean.FeeListBean;
import com.xinfu.newproject.bean.park_bean.bean.GetMonthCarListBean;
import com.xinfu.newproject.bean.park_bean.bean.GetParkingBean;
import com.xinfu.newproject.bean.park_bean.bean.MonthCarPayBean;
import com.xinfu.newproject.bean.park_bean.bean.PaymentsBean;
import com.xinfu.newproject.bean.park_bean.bean.TempPayBean;
import com.xinfu.newproject.bean.park_bean.body.AddMonthCarBody;
import com.xinfu.newproject.bean.park_bean.body.AppIntoPayBody;
import com.xinfu.newproject.bean.park_bean.body.CheckMonthParkBody;
import com.xinfu.newproject.bean.park_bean.body.FeeListBody;
import com.xinfu.newproject.bean.park_bean.body.GetMonthCarListBody;
import com.xinfu.newproject.bean.park_bean.body.GetParkingBody;
import com.xinfu.newproject.bean.park_bean.body.PaymentsBody;
import com.xinfu.newproject.bean.park_bean.body.TempPayBody;
import com.xinfu.newproject.bean.paycost.bean.ComputeParkingBean;
import com.xinfu.newproject.bean.paycost.body.ComputeParkingBody;
import com.xinfu.newproject.callback.MyCallBack;
import com.xinfu.newproject.model.IModel;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class ParkingModel implements IModel.IParkingModel {

    /**
     * 查询停车场
     *
     * @param getParkingBody
     * @param callBack
     */
    @Override
    public void getParking(GetParkingBody getParkingBody, MyCallBack<GetParkingBean> callBack) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", getParkingBody.getUserId());

        JsNetworkService.getInstance().getIJsNetworkService()
                .getParkingLot(getParkingBody.getHeader(), getParkingBody.getUserId())
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<GetParkingBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Disposable mDisposable; //用于Activity销毁时停止执行
                        mDisposable = d;
                        callBack.onDispose(mDisposable);
                    }

                    @Override
                    public void onNext(@NonNull GetParkingBean getParkingBean) {

                        callBack.onSuccess(getParkingBean);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        callBack.onError("Server Error", "网络异常，请稍后再试");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 续费记录
     *
     * @param feeListBody
     * @param callBack
     */
    @Override
    public void feeList(FeeListBody feeListBody, MyCallBack<FeeListBean> callBack) {
        Map<String, Object> map = new HashMap<>();
        map.put("parkCarId", feeListBody.getParkCarId());

        JsNetworkService.getInstance2().getIJsNetworkService()
                .feeList(feeListBody.getHeader(), map)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<FeeListBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Disposable mDisposable; //用于Activity销毁时停止执行
                        mDisposable = d;
                        callBack.onDispose(mDisposable);
                    }

                    @Override
                    public void onNext(@NonNull FeeListBean feeListBean) {
                        callBack.onSuccess(feeListBean);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        callBack.onError("Server Error", "网络异常，请稍后再试");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 根据车场id和车牌号检查车辆信息
     *
     * @param
     * @param callBack
     */
    @Override
    public void checkMonthParkCar(CheckMonthParkBody checkMonthParkBody, MyCallBack<CheckMonthParkBean> callBack) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", checkMonthParkBody.getUserId());
        map.put("parkInfoId", checkMonthParkBody.getParkInfoId());
        map.put("carId", checkMonthParkBody.getCarId());
        map.put("carType", checkMonthParkBody.getCarType());

        JsNetworkService.getInstance2().getIJsNetworkService()
                .checkMonthParkCar(checkMonthParkBody.getHeader(), map)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CheckMonthParkBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Disposable mDisposable; //用于Activity销毁时停止执行
                        mDisposable = d;
                        callBack.onDispose(mDisposable);
                    }

                    @Override
                    public void onNext(@NonNull CheckMonthParkBean checkMonthParkBean) {

                        callBack.onSuccess(checkMonthParkBean);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        callBack.onError("Server Error", "网络异常，请稍后再试");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 添加月卡车
     *
     * @param
     * @param callBack
     */
    @Override
    public void addMonthCar(AddMonthCarBody addMonthCarBody, MyCallBack<AddMonthCarBean> callBack) {
        Map<String, Object> map = new HashMap<>();
        map.put("projectId", addMonthCarBody.getProjectId());
        map.put("exclusiveFlag", addMonthCarBody.getExclusiveFlag());
        map.put("addUserId", addMonthCarBody.getUserId());
        map.put("parkInfoId", addMonthCarBody.getParkInfoId());
        map.put("carId", addMonthCarBody.getCarId());
        map.put("monthCarType", addMonthCarBody.getMonthCarType());
        map.put("roomId", addMonthCarBody.getRoomId());
        map.put("owner", addMonthCarBody.getOwner());
        map.put("mobile", addMonthCarBody.getMobile());
        map.put("images", addMonthCarBody.getImages());
        map.put("remark", addMonthCarBody.getRemark());
        map.put("idType", addMonthCarBody.getIdType());
        map.put("carType", addMonthCarBody.getCarType());
        map.put("filed1", addMonthCarBody.getFiled1());
        map.put("tenantId", addMonthCarBody.getHeader());

        JsNetworkService.getInstance2().getIJsNetworkService()
                .addMonthCar(addMonthCarBody.getHeader(), map)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<AddMonthCarBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Disposable mDisposable; //用于Activity销毁时停止执行
                        mDisposable = d;
                        callBack.onDispose(mDisposable);
                    }

                    @Override
                    public void onNext(@NonNull AddMonthCarBean addMonthCarBean) {

                        callBack.onSuccess(addMonthCarBean);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        callBack.onError("Server Error", "网络异常，请稍后再试");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 根据用户id查询月卡车列表
     *
     * @param
     * @param callBack
     */
    @Override
    public void getMonthCarList(GetMonthCarListBody getMonthCarListBody, MyCallBack<GetMonthCarListBean> callBack) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", getMonthCarListBody.getUserId());
        map.put("carType", getMonthCarListBody.getCarType());

        JsNetworkService.getInstance2().getIJsNetworkService()
                .getMonthCarList(getMonthCarListBody.getHeader(), map)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<GetMonthCarListBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Disposable mDisposable; //用于Activity销毁时停止执行
                        mDisposable = d;
                        callBack.onDispose(mDisposable);
                    }

                    @Override
                    public void onNext(@NonNull GetMonthCarListBean getMonthCarListBean) {

                        callBack.onSuccess(getMonthCarListBean);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        callBack.onError("Server Error", "网络异常，请稍后再试");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 支付方式查询
     *
     * @param
     * @param callBack
     */
    @Override
    public void payments(PaymentsBody paymentsBody, MyCallBack<PaymentsBean> callBack) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", paymentsBody.getId());
//        map.put("startTime", paymentsBody.getStartTime());
//        map.put("orderId", paymentsBody.getOrderId());

        JsNetworkService.getInstance2().getIJsNetworkService()
                .payments(paymentsBody.getHeader(), map)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<PaymentsBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Disposable mDisposable; //用于Activity销毁时停止执行
                        mDisposable = d;
                        callBack.onDispose(mDisposable);
                    }

                    @Override
                    public void onNext(@NonNull PaymentsBean paymentsBean) {

                        callBack.onSuccess(paymentsBean);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        callBack.onError("Server Error", "网络异常，请稍后再试");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void appIntoPay(AppIntoPayBody appIntoPayBody, MyCallBack<MonthCarPayBean> callBack) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", appIntoPayBody.getUserId());
        map.put("carId", appIntoPayBody.getCarId());
        map.put("carType", appIntoPayBody.getCarType());
        map.put("parkCarId", appIntoPayBody.getParkCarId());
        map.put("startTime", appIntoPayBody.getStartTime());
        map.put("num", appIntoPayBody.getNum());
        map.put("payAmount", appIntoPayBody.getPayAmount());
        map.put("payMemo", appIntoPayBody.getPayMemo());
        map.put("mobile", appIntoPayBody.getMobile());
        map.put("projectId", appIntoPayBody.getProjectId());
        map.put("tenantId", appIntoPayBody.getTenantId());

        Log.i("testAppIntoPay", new Gson().toJson(map));

        JsNetworkService.getInstance2().getIJsNetworkService()
                .appIntoPay(appIntoPayBody.getHeader(), map)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<MonthCarPayBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Disposable mDisposable; //用于Activity销毁时停止执行
                        mDisposable = d;
                        callBack.onDispose(mDisposable);
                    }

                    @Override
                    public void onNext(@NonNull MonthCarPayBean monthCarPayBean) {

                        callBack.onSuccess(monthCarPayBean);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        callBack.onError("Server Error", "网络异常，请稍后再试");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void ComputeParkingFee(ComputeParkingBody computeParkingBody, MyCallBack<ComputeParkingBean> callBack) {
        JsNetworkService.getInstance3().getIJsNetworkService()
                .ComputeParkingFee(computeParkingBody.getLicensePlate())
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ComputeParkingBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Disposable mDisposable; //用于Activity销毁时停止执行
                        mDisposable = d;
                        callBack.onDispose(mDisposable);
                    }

                    @Override
                    public void onNext(@NonNull ComputeParkingBean computeParkingBean) {

                        callBack.onSuccess(computeParkingBean);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        callBack.onError("Server Error", "网络异常，请稍后再试");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 临停支付
     *
     * @param tempPayBody
     * @param callBack
     */

    @Override
    public void tempPay(TempPayBody tempPayBody, MyCallBack<TempPayBean> callBack) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", tempPayBody.getUserId());
        map.put("mobile", tempPayBody.getMobile());
        map.put("carType", tempPayBody.getCarType());
        map.put("carId", tempPayBody.getCarId());
        map.put("PKName", tempPayBody.getPKName());
        map.put("payAmount", tempPayBody.getPayAmount());
        map.put("PKID", tempPayBody.getPKID());
        map.put("tempOrderNo", tempPayBody.getTempOrderNo());

        Log.i("testAppIntoPay", new Gson().toJson(map));

        JsNetworkService.getInstance3().getIJsNetworkService()
                .tempPay(tempPayBody.getHeader(), map)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<TempPayBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Disposable mDisposable; //用于Activity销毁时停止执行
                        mDisposable = d;
                        callBack.onDispose(mDisposable);
                    }

                    @Override
                    public void onNext(@NonNull TempPayBean tempPayBean) {

                        callBack.onSuccess(tempPayBean);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        callBack.onError("Server Error", "网络异常，请稍后再试");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
